<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Care_about_others extends CI_Controller
{
	private function get_friend_field_value($friendUid,$field, $infoId)
	{
		$this->db->where("healthInfoId", $infoId);
		$query = $this->db->get("userhealthinfo");
		
		$friend_health_info = $query->row_array();
		
		return $friend_health_info[$field];
	}
	
	private function get_push_health_notice()
	{
		//query condition about date can be put here!
		$this->load->model('customer_health_model');
		
		$this->db->where("friendUid", get_uid());
		$query = $this->db->get("userhealthinfopushnotice");
		$result = $query->result();
		
		if(!$result)
			return array();
		
		$friend_update = array();
		foreach($result as $row)
		{
			if($this->customer_health_model->is_health_info_shared_by($row->uid, $row->column))
			{
				if(!array_key_exists($row->uid,$friend_update))
					$friend_update[$row->uid] = array();			
				$friend_info = & $friend_update[$row->uid];
				$friend_info[] = array(
						"field"=>$row->column,
						"value"=>$this->get_friend_field_value($row->uid, $row->column, $row->infoId),
						"date" =>$row->date
						);
			}
		}
		
		return $friend_update;
	}
	
	private function get_push_symptom_notice()
	{
		$this->load->model('personal_health_info_model');
		$this->db->where("friendUid", get_uid());
		$query = $this->db->get("usersymptompushnotice");
		$result = $query->result();
	
		if(!$result)
			return array();
	
		$friend_update = array();
		foreach($result as $row)
		{
			$this->db->where("symptomInfoId", $row->symptomInfoId);
			$query = $this->db->get("usersymptom");
				
			$symptomType = $query->row()->symptomType;
			$date = $query->row()->startDate;
			
			if($this->personal_health_info_model->is_symptom_shared_by($row->uid, $symptomType))
			{
				if(!array_key_exists($row->uid,$friend_update))
					$friend_update[$row->uid] = array();		
				$friend_info = & $friend_update[$row->uid];			
				$friend_info[] = array("date"=>$date, "symptomType"=>$symptomType);
			}
		
		}
	
		return $friend_update;
	}
	
	public function index()
	{
		$friend_health_update = $this->get_push_health_notice();
		$friend_symptom_update = $this->get_push_symptom_notice();
		
		if(!$friend_health_update && !$friend_symptom_update){
			$this->load->view("functionEntry/careAboutOthers/careAboutOthers");
			return;
		}
			
		$data = array(
				"friend_health_update"=>$friend_health_update,
				"friend_symptom_update"=>$friend_symptom_update
				);
		
		$this->load->view("functionEntry/careAboutOthers/careAboutOthers", $data);
	}
}
?>